<?php
namespace app\user\service;

use app\user\validate\UserValidate;
use tp5_lib\user\logic\UserLogic;

class UserService
{
    protected $error="";
    public function getError()
    {
        return $this->error;
    }
    /** @return UserValidate */
    public function getValidate()
    {
        if(empty($this->_validate)){
            $this->_validate=new UserValidate();
        }
        return $this->_validate;
    }
    /** @return UserLogic */
    public function getLogic()
    {
        if(empty($this->_logic)){
            $this->_logic=new UserLogic();
        }
        return $this->_logic;
    }
    //修改用户信息(基本信息)
    public function updateInfo($user,$data)
    {
        if(!$this->getLogic()->updateProfile($user,$data)){
            $this->error=$this->getLogic()->getError();
            return false;
        }
        return $this;
    }
    //修改用户手机号
    public function updateMobile($user,$data)
    {
        if(!$this->getValidate()->scene("update_mobile")->check($data)){
            $this->error=$this->getValidate()->getError();
            return false;
        }
        if(!$this->getLogic()->bindMobile($user,$data["mobile"],$data["code"])){
            $this->error=$this->getLogic()->getError();
            return false;
        }
        return $this;
    }
    //修改用户密码
    public function updatePassword($user,$data)
    {
        if(!$this->getValidate()->scene("update_password")->check($data)){
            $this->error=$this->getValidate()->getError();
            return false;
        }
        if(!$this->getLogic()->resetPassword($user,$data["old_password"],$data["new_password"])){
            $this->error=$this->getLogic()->getError();
            return false;
        }
        return $this;
    }
}
